const {SyncLoopHook} = require('tapable');
//当回调函数返回非undefined值的时候会停止调用后续的回调
class SyncWaterfallHook{
  constructor(args){
    this._args = args;//['name','age']
    this.taps = [];
  }
  tap(name,fn){
     this.taps.push(fn);
  }
  call(){
      let args = Array.from(arguments).slice(0,this._args.length);
      let result;
      let last;
      for(let i=0;i<this.taps.length;i++){
          result = this.taps[i](last||args[0],...args.slice(1));
          if(result)last=result;
      }
  }

}
let hook = new SyncLoopHook(['name','age']);
let counter1 = 0;
let counter2 = 0;
let counter3 = 0;
//不停的循环执行回调函数，直到函数结果等于undefined
hook.tap('1',(name,age)=>{
  console.log(1,name,age);
  if(++counter1==1){
      counter1 = 0
      return;
  }
  return true;
});
hook.tap('2',(name,age)=>{
  console.log(2,name,age);
  if(++counter2==2){
      counter2 = 0
      return;
  }
  return true;
});
hook.tap('3',(name,age)=>{
  console.log(3,name,age);
  if(++counter3==3){
      counter3 = 0;
      return;
  }
  return true;
});
//hook.emit();hook.trigger();
hook.call('zhufeng',10);

